package com.topera.eduservice.controller;

import com.topera.commonutils.R;
import com.topera.eduservice.entity.EduChapter;
import com.topera.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-05-18
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 根据课程id查找课程大纲信息
     * @param courseId
     * @return
     */
    @GetMapping("getAllChapterBycourseId/{courseId}")
    public R getAllChapterBycourseId(@PathVariable String courseId){

        List<EduChapter> chapterList = eduChapterService.getAllChapterBycourseId(courseId);
        return R.ok().data("chapterList",chapterList);
    }

    /**
     * 保存章节信息
     * @param chapter
     * @return
     */
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter){
        eduChapterService.save(chapter);
        return R.ok();
    }

    /**
     * 获取指定章节信息
     * @param id
     * @return
     */
    @GetMapping("getChapter/{id}")
    public R getChapter(@PathVariable String id){
        EduChapter chapter = eduChapterService.getById(id);
        return R.ok().data("chapter",chapter);
    }

    /**
     * 更新章节信息
     * @param chapter
     * @return
     */
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter){
        eduChapterService.updateById(chapter);
        return R.ok();
    }

    /**
     * 删除章节信息
     * @param id
     * @return
     */
    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id){
        eduChapterService.removeById(id);
        return R.ok();
    }

}

